package com.xxhui.recyclerptrdemo.Util;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;

import com.xxhui.recyclerptrdemo.Adapter.RecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * Created by Administrator on 2016/8/5.
 */
public class Recycler_ptr_util {

    private static final String TAG = "Recycler_ptr_util" ;

    public static void updateData(List<String> data, RecyclerViewAdapter adapter, PtrClassicFrameLayout mPtrFrame){
        data.add("updateDate");
        mPtrFrame.refreshComplete();//记得设置完成
        adapter.notifyDataSetChanged();
    }

    public static List<String> getData(){
        List<String> date = new ArrayList<String>();
        for(int i=1 ; i<=10 ; i++){
            date.add("date"+i);
        }
        return date;
    }

    public static boolean checkRecyclerViewCanDoRefresh(RecyclerView recyclerView){

        int count = recyclerView.getChildCount();
        Log.i(TAG, "checkRecyclerViewCanDoRefresh: ChildCount:"+count);
        if (count == 0) {
            return true;
        }

        int top = recyclerView.getChildAt(0).getTop();
        Log.i(TAG, "checkRecyclerViewCanDoRefresh: top:"+top);
        if (top > 10) {//不知道为什么我手机有6像素，所以这样子比较通用把，有个范围
            return false;
        }
        final RecyclerView inner_recyclerView =  recyclerView;
        RecyclerView.LayoutManager layoutManager = inner_recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            int position = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
            //Log.i(TAG, "checkRecyclerViewCanDoRefresh: first_position："+position);
            if (position == 0) {
                return true;
            } else if (position == -1) {
                position = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                return position == 0;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            boolean allViewAreOverScreen = true;
            int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
            for (int i = 0; i < positions.length; i++) {
                if (positions[i] == 0) {
                    return true;
                }
                if (positions[i] != -1) {
                    allViewAreOverScreen = false;
                }
            }
            if (allViewAreOverScreen) {
                positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
                for (int i = 0; i < positions.length; i++) {
                    if (positions[i] == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
